﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using sexhealth.console.service.post;
using sexhealth.console.service.post.vo;
using System.Text;
using com.pro2e.web.util.page;
public partial class admin_UC05_0502boardcastmaintain : System.Web.UI.Page
{

    PostService postService = new PostService();
    //廣播分享
    int channelId = 15;
    private int PostMode
    {
        get { if (ViewState["postmode"] == null) { ViewState["postmode"] = 0; } return int.Parse(ViewState["postmode"].ToString()); }
        set { ViewState["postmode"] = value; }
    }
    private string FileName
    {
        get { if (ViewState["filename"] == null) { ViewState["filename"] = string.Empty; } return ViewState["filename"].ToString(); }
        set { ViewState["filename"] = value; }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            showListMode();
            GetList();

        }
    }
    protected void ddlChannel_SelectedIndexChanged(object sender, EventArgs e)
    {
        GetList();
    }
    protected void btnAddNew_Click(object sender, EventArgs e)
    {
        PostMode = 0;
        showModifyMode();
    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        showListMode();
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        HttpFileCollection hfc = Request.Files;
        if (hfc.Count > 0)
        {
            hfc[0].SaveAs(Server.MapPath("~\\") + "\\upload\\media\\" + System.IO.Path.GetFileName(hfc[0].FileName));
            FileName = System.IO.Path.GetFileName(hfc[0].FileName);
        }
        if (PostMode == 0)
        {

            PostVO post = new PostVO();
            post.Title = txtTitle.Text.Trim();
            post.KeyWord = txtKeyWord.Text;
            post.HtmlContent = ckeDescription.value;
            post.BelongToChannelId = channelId;
            post.FileLink = FileName;
            postService.InsertPost(post);

        }
        else
        {

            PostVO post = postService.DaoGetVOById<PostVO>(PostMode);
            post.Title = txtTitle.Text.Trim();
            post.KeyWord = txtKeyWord.Text;
            post.HtmlContent = ckeDescription.value;
            post.BelongToChannelId = channelId;
            if (hfc.Count > 0)
            {
                post.FileLink = FileName;
            }
            postService.UpdatePost(post);
        }
        showListMode();
        GetList();

    }
    private void showListMode()
    {
        pnlGv.Visible = true;
        pnlContent.Visible = false;
        btnAddNew.Visible = true;
    }
    private void showModifyMode()
    {
        pnlGv.Visible = false;
        pnlContent.Visible = true;
        btnAddNew.Visible = false;
    }
    private void GetList()
    {

        gvResult.DataSource = postService.GetPostList(channelId);
        gvResult.DataBind();

    }
    protected void gvResult_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        string cmdName = e.CommandName;
        int PostNo = int.Parse(e.CommandArgument.ToString());
        PostVO post = postService.DaoGetVOById<PostVO>(PostNo);
        switch (cmdName)
        {
            case "myModify":

                txtTitle.Text = post.Title;
                txtKeyWord.Text = post.KeyWord;
                ckeDescription.value = post.HtmlContent;
                showModifyMode();
                ShowContent();
                PostMode = PostNo;
                break;

            case "myDel":
                postService.DaoDelete(post);
                GetList();
                break;

            default:
                break;
        }
    }
    private void ShowContent()
    {
        IList<PostVO> postList = postService.GetPostList(channelId);
        if (postList.Count > 0)
        {
            txtTitle.Text = postList[0].Title;
            ltlPreview.Text = " <audio src='../../upload/media/" + postList[0].FileLink + "' controls> ";

        }

    }


}
